package com.hujing.springsource.a03;

import java.util.ArrayList;
import java.util.List;

/**
 * 该测试类是用来学习spring中的模板方法
 * 1.定义一个MyBeanFactory类
 * 2.定义接口BeanPostProcessor，该接口有一个方法inject()，该方法用来动态实现其他处理逻辑
 */
public class TestMethodTemplate {

    public static void main(String[] args) {
        MyBeanFactory myBeanFactory = new MyBeanFactory();
        myBeanFactory.addBeanPostProcessor(() -> System.out.println("解析@Autowired注解"));
        myBeanFactory.addBeanPostProcessor(() -> System.out.println("解析@Resource注解"));
        myBeanFactory.getBean();
    }
    
    static class MyBeanFactory{
        public Object getBean(){
            Object bean = new Object();
            System.out.println("构造"+bean);
            System.out.println("依赖注入"+bean);
            for (BeanPostProcessor processor : processors) {
                processor.inject();
            }
            System.out.println("初始化"+bean);
            return bean;
        }
        
        private final List<BeanPostProcessor> processors = new ArrayList<>();
        
        public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor){
            processors.add(beanPostProcessor);
        }
    }
    
    interface BeanPostProcessor{
        void inject();
    }
}
